Class No.18  Data Structures http://ecomputernotes.com
Reference Variables One should be careful about transient objects that are stored by reference in data structures. Consider the following code that stores and retrieves objects in a queue.  http://ecomputernotes.com
Reference Variables void loadCustomer( Queue& q) { Customer c1(“irfan”); Customer c2(“sohail”; q.enqueue( c1 ); q.enqueue( c2 ); } http://ecomputernotes.com
Reference Variables void serviceCustomer( Queue& q) { Customer c = q.dequeue(); cout << c.getName() << endl; } We got the reference but the object is gone! The objects were created on the call stack. They disappeared when the loadCustomer function returned. http://ecomputernotes.com
Reference Variables void loadCustomer( Queue& q) { Customer* c1 = new Customer(“irfan”); Customer* c2 = new Customer(“sohail”; q.enqueue( c1 ); // enqueue takes pointers q.enqueue( c2 ); } The pointer variables c1 and c2 are on the call stack. They will go but their contents (addresses) are queued. The Customer objects are created in the heap. They will live until explicitly deleted. http://ecomputernotes.com
Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (ourtest.exe) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
Reference Variables Call stack layout when q.enqueue(c2) called in loadCustomer. 1052 1060 1068 1056 600 1072 624 . . . c1 c2 loadCustomer enqueue stack grows downwards   sp (elt) (624) elt
Reference Variables Heap layout during call to loadCustomer. 600 624 c1 c2 Customer(“irfan”)  ->  c1 heap grows upwards   648 Customer(“sohail”)  ->  c2 irfan sohail http://ecomputernotes.com
Reference Variables void serviceCustomer( Queue& q) { Customer* c = q.dequeue(); cout << c->getName() << endl; delete c; // the object in heap dies } Must use the c-> syntax because we get a pointer from the queue. The object is still alive because it was created in the heap. http://ecomputernotes.com
The const Keyword The const keyword is often used in function signatures. The actual meaning depends on where it occurs but it generally means something is to held constant. Here are some common uses. http://ecomputernotes.com
The const Keyword Use 1 : The const keyword appears before a function parameter. E.g., in a chess program: int movePiece(const Piece& currentPiece)  The parameter must remain constant for the life of the function. If you try to change the value, e.g., parameter appears on the left hand side of an assignment, the compiler will generate and error. http://ecomputernotes.com
The const Keyword This also means that if the parameter is passed to another function, that function must not change it either.  Use of const with reference parameters is very common. This is puzzling; why are we passing something by reference and then make it constant, i.e., don’t change it? Doesn’t passing by reference mean we  want  to change it? http://ecomputernotes.com

computer notes - Data Structures - 18

  • 1.
    Class No.18 Data Structures http://ecomputernotes.com
  • 2.
    Reference Variables Oneshould be careful about transient objects that are stored by reference in data structures. Consider the following code that stores and retrieves objects in a queue. http://ecomputernotes.com
  • 3.
    Reference Variables voidloadCustomer( Queue& q) { Customer c1(“irfan”); Customer c2(“sohail”; q.enqueue( c1 ); q.enqueue( c2 ); } http://ecomputernotes.com
  • 4.
    Reference Variables voidserviceCustomer( Queue& q) { Customer c = q.dequeue(); cout << c.getName() << endl; } We got the reference but the object is gone! The objects were created on the call stack. They disappeared when the loadCustomer function returned. http://ecomputernotes.com
  • 5.
    Reference Variables voidloadCustomer( Queue& q) { Customer* c1 = new Customer(“irfan”); Customer* c2 = new Customer(“sohail”; q.enqueue( c1 ); // enqueue takes pointers q.enqueue( c2 ); } The pointer variables c1 and c2 are on the call stack. They will go but their contents (addresses) are queued. The Customer objects are created in the heap. They will live until explicitly deleted. http://ecomputernotes.com
  • 6.
    Memory Organization CodeStatic data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (ourtest.exe) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
  • 7.
    Reference Variables Callstack layout when q.enqueue(c2) called in loadCustomer. 1052 1060 1068 1056 600 1072 624 . . . c1 c2 loadCustomer enqueue stack grows downwards sp (elt) (624) elt
  • 8.
    Reference Variables Heaplayout during call to loadCustomer. 600 624 c1 c2 Customer(“irfan”) -> c1 heap grows upwards 648 Customer(“sohail”) -> c2 irfan sohail http://ecomputernotes.com
  • 9.
    Reference Variables voidserviceCustomer( Queue& q) { Customer* c = q.dequeue(); cout << c->getName() << endl; delete c; // the object in heap dies } Must use the c-> syntax because we get a pointer from the queue. The object is still alive because it was created in the heap. http://ecomputernotes.com
  • 10.
    The const KeywordThe const keyword is often used in function signatures. The actual meaning depends on where it occurs but it generally means something is to held constant. Here are some common uses. http://ecomputernotes.com
  • 11.
    The const KeywordUse 1 : The const keyword appears before a function parameter. E.g., in a chess program: int movePiece(const Piece& currentPiece) The parameter must remain constant for the life of the function. If you try to change the value, e.g., parameter appears on the left hand side of an assignment, the compiler will generate and error. http://ecomputernotes.com
  • 12.
    The const KeywordThis also means that if the parameter is passed to another function, that function must not change it either. Use of const with reference parameters is very common. This is puzzling; why are we passing something by reference and then make it constant, i.e., don’t change it? Doesn’t passing by reference mean we want to change it? http://ecomputernotes.com

Editor's Notes

  • #8 End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #9 End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #13 End of lecture 18